package com.navinfo.opentsp.platform.computing.service.huashen;

import com.alibaba.fastjson.JSON;
import com.navinfo.opentsp.platform.computing.anno.RoutingProtocol;
import com.navinfo.opentsp.platform.computing.enums.ProtocolEnum;
import com.navinfo.opentsp.platform.computing.enums.YardEnum;
import com.navinfo.opentsp.platform.computing.service.IProtocolParseService;
import com.navinfo.platform.bigdata.dto.RealTime0F3BData;
import org.apache.flume.Event;
import org.msgpack.MessagePack;

@RoutingProtocol(yard= YardEnum.HUASHEN, protocolType = ProtocolEnum.REALTIME_DATA)
public class RealTime0F3BHuaShenService implements IProtocolParseService {

    private MessagePack msgpack = new MessagePack();
    //如果因为反序列化单利效率低，可以采用本地线程方式
//    private ThreadLocal<MessagePack> messagePackThreadLocal = new ThreadLocal<MessagePack>(){
//        @Override
//        protected MessagePack initialValue() {
//            return new MessagePack();
//        }
//    };

    @Override
    public String parseProtocol(Event event) {
        try {
            //拼写落盘到HDFS的字符串。字段间分隔符是\001，null通过\N表示
//            RealTime0F3BData realTime0F3BData = messagePackThreadLocal.get().read(event.getBody(), RealTime0F3BData.class);
            RealTime0F3BData realTime0F3BData = msgpack.read(event.getBody(), RealTime0F3BData.class);
            if(realTime0F3BData.getStaticsDataMap().isEmpty()){
                realTime0F3BData.setStaticsDataMap(null);
            }
            return JSON.toJSONString(realTime0F3BData);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
